<template>
  <el-dialog
    title="流程图标"
    :visible="visible"
    :append-to-body="true"
    @close="handleDialogClose"
    width="600px"
  >
    <div class="icons">
      <div
        class="icon-item"
        :class="{active: item === activeIcon}"
        v-for="item of iconList"
        :key="item"
        @click="activeIcon = item"
      >
        <svg-icon :iconClass="item" />
      </div>
    </div>
    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import FlowInfoService from '../../../../service/flow_info'

export default {
  props: {
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    }
  },
  data() {
    return {
      visible: false,
      iconList: [],
      activeIcon: ''
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 初始化图标
    init() {
      const context = require.context('../../../../icons/svg', false, /\.svg$/)
      context
        .keys()
        .forEach(item =>
          this.iconList.push(context(item).default.id.substring(5))
        )
    },
    // 处理Dialog关闭事件
    handleDialogClose() {
      this.$emit('update:show')
    },
    // 提交表单
    submitForm() {
      const flow = {
        flowKey: this.data.key,
        iconName: this.activeIcon
      }

      FlowInfoService.updateFlowInfo(flow).then(res => {
        this.$message.success('设置成功')
        this.visible = false
        this.$emit('success')
      })
    }
  },
  watch: {
    show(value) {
      if (value) {
        this.activeIcon =
          (this.data.flowInfo && this.data.flowInfo.iconName) || ''
      }
      this.visible = value
    }
  }
}
</script>

<style lang="stylus" scoped>
.icons
  display flex
  flex-wrap wrap
  .icon-item
    text-align center
    padding 8px
    margin 0.3rem
    box-sizing border-box
    border 1px solid #f1f1f1
    border-radius 2px
    cursor pointer
    font-size 18px
    &.active, &:hover
      border-color #4a4a48
      background #4a4a48
      color #ffffff
      transition all 0.3s
</style>
